<template>
  <div id="home" ref="home">
    <!--1.导航栏部分 -->
    <nav-bar class="home-nav">
      <template slot="center">购物街</template>
    </nav-bar>
    <!-- 2.列表切换条 -->
    <tab-control
      @tabclick="tabclick"
      :titles="['流行', '新款', '精选']"
      ref="toptabcontrol"
      class="tab-control"
      v-show="isTabFixed"
    />
    <!-- 3.可滚动区域(Better-Scroll优化滚动效果123) -->
    <scroll
      class="contents"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
      :pullUpLoad="true"
      @pullingUp="loadMore"
    >
      <!-- 3.1 轮播图 -->
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad" />
      <!-- 3.2 推荐区域 -->
      <recommend-view :recommends="recommends" />
      <!-- 3.3 本周流行 -->
      <feature></feature>
      <!-- 3.4 列表切换条 -->
      <tab-control
        @tabclick="tabclick"
        :titles="['流行', '新款', '精选']"
        ref="tabcontrol"
      />
      <!-- 3.5 商品列表 -->
      <goods-list :goods="showGoods"> </goods-list>
    </scroll>
    <!-- 4. 返回顶部组件 -->
    <back-top @click.native="backclick" v-show="isShowBackTop"> </back-top>
  </div>
</template>

<script>
import { getHomeMultidata, getHomeGoods } from "network/home";
import HomeSwiper from "./childComps/HomeSwiper.vue";
import RecommendView from "./childComps/RecommendView.vue";
import Feature from "./childComps/FeatureView.vue";
import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabcontrol/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
import BackTop from "components/content/backTop/BackTop";
import { itemListenerMixin } from "common/mixin";

export default {
  name: "Home",
  components: {
    HomeSwiper,
    RecommendView,
    Feature,
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  minxin: [itemListenerMixin],
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      isShowBackTop: false,
      taboffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  computed: {
    //根据当前商品的类型动态显示页面
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  created() {
    //页面创建时进行网络请求
    this.getHomeMultidata();
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  activated() {
    //当前组件被激活时scroll组件重新刷新
    this.$refs.scroll.refresh();
    //当前组件被激活时滚动到上次保留的位置
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
  },
  deactivated() {
    //离开当前组件记录当前位置
    this.saveY = this.$refs.scroll.getCurrentY();
    //在事件总线中关闭图片加载的事件监听
    this.$bus.$off("itemImageLoad", this.ItemImgListener);
  },
  methods: {
    // 1.事件监听相关
    tabclick(index) {
      //点击切换栏时候记录goods对象中当前索引号对应的类型
      this.currentType = Object.keys(this.goods)[index];
    },
    backclick() {
      // 点击返回顶部按钮，跳转到指定位置
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    contentScroll(position) {
      // 监听滚动事件， 如果当前位置大于1000，则显示返回顶部按钮
      this.isShowBackTop = -position.y > 1000;
      // 如果当前位置大于切换栏距离顶部的距离。则让Tab切换栏变为fixed吸顶显示
      this.isTabFixed = -position.y > this.taboffsetTop;
    },
    loadMore() {
      // 监听上拉事件回调商品请求函数并刷新scroll重新计算高度
      this.getHomeGoods(this.currentType);
      this.$refs.scroll.refresh();
    },
    //2.网络请求相关
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.data.banner.list;
        this.recommends = res.data.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      // 当前页数为初始页面+1
      const page = this.goods[type].page + 1;
      // 请求商品类别数据
      getHomeGoods(type, page).then((res) => {
        console.log(res);
        this.goods[type].list.push(...res.data.data.list);
        // 请求一次数据后自动将page+1并调用scroll完成上拉的函数
        this.goods[type].page += 1;
        this.$refs.scroll.finishPullUp();
      });
    },
    swiperImageLoad() {
      //等轮播图加载完成后记录切换栏的offsetTop值比较准确
      this.taboffsetTop = this.$refs.tabcontrol.$el.offsetTop;
    },
  },
};
</script>

<style scoped>
#home {
  height: 100vh;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
}
.tab-control {
  position: relative;
  top: 44px;
  left: 0;
  top: 0;
  background-color: #fff;
  z-index: 10;
}

.contents {
  position: absolute;
  top: 44px;
  left: 0;
  right: 0;
  height: calc(100% - 93px);
  overflow: hidden;
}
</style>